package com.heima.media.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.common.dto.PageResponseResult;
import com.heima.common.dto.ResponseResult;
import com.heima.common.dto.User;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.common.minio.MinIOService;
import com.heima.common.util.MediaThreadLocalUtil;
import com.heima.media.dto.WmMaterialDto;
import com.heima.media.entity.WmMaterial;
import com.heima.media.mapper.WmMaterialMapper;
import com.heima.media.service.IWmMaterialService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;

/**
 * <p>
 * 自媒体图文素材信息表 服务实现类
 * </p>
 *
 * @author mcm
 * @since 2022-06-22
 */
@Service
public class WmMaterialServiceImpl extends ServiceImpl<WmMaterialMapper, WmMaterial> implements IWmMaterialService {

    @Autowired
    private MinIOService minIOService;
    @Override
    public ResponseResult<WmMaterial> upload(MultipartFile file) {
        User user = MediaThreadLocalUtil.get();
        if (user==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        String url = minIOService.upload(file);
        WmMaterial material=new WmMaterial();
        material.setUserId(user.getUserId());
        material.setUrl(url);
        material.setType(0);
        material.setIsCollection(0);
        material.setCreatedTime(new Date());
        this.save(material);
        return ResponseResult.okResult(material);
    }

    @Override
    public ResponseResult liat(WmMaterialDto wmMaterialDto) {
        User user = MediaThreadLocalUtil.get();
        if (user==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        Page<WmMaterial> page = new Page<>(wmMaterialDto.getPage(), wmMaterialDto.getSize());
        LambdaQueryWrapper<WmMaterial> query = new LambdaQueryWrapper<>();
        if (wmMaterialDto.getIsCollection()!=null){
            query.eq(WmMaterial::getIsCollection,wmMaterialDto.getIsCollection());
        }
        query.eq(WmMaterial::getUserId,user.getUserId());
        query.orderByDesc(WmMaterial::getCreatedTime);
        Page<WmMaterial> page1 = this.page(page, query);
        PageResponseResult result=new PageResponseResult(wmMaterialDto.getPage(),wmMaterialDto.getSize(),page1.getTotal(),page1.getRecords()
        );

        return result;

    }

    /**
     * 素材管理表: 添加收藏
     * @param id
     * @return
     */
    @Override
    public ResponseResult collectById(Integer id) {
        WmMaterial byId = this.getById(id);
        byId.setIsCollection(1);
        this.updateById(byId);
        return  ResponseResult.okResult();
    }

    /**
     * 素材管理表: 取消收藏 (1收藏, 0 不收藏)
     * @param id
     * @return
     */
    @Override
    public ResponseResult collectById1(Integer id) {
        WmMaterial byId = this.getById(id);
        byId.setIsCollection(0);
        this.updateById(byId);

        return ResponseResult.okResult();

    }
}
